<template>
    <div class="hero">
        <div class="hero-text">JOURNEO</div>
        <div class="main-box">
            <span class="sp sp-t"></span>
            <span class="sp sp-r"></span>
            <span class="sp sp-b"></span>
            <span class="sp sp-l"></span>
            <div class="form-box">
                <div class="button-box">
                    <div class="btn-slide" :style="{ left: isLogin ? '0' : '50%' }"></div>
                    <button type="button" class="toggle-btn" :class="{ active: isLogin }"
                        @click="isLogin = true">LOGIN</button>
                    <button type="button" class="toggle-btn" :class="{ active: !isLogin }"
                        @click="isLogin = false">REGISTER</button>
                </div>

                <div class="social-icons">
                    <a class="icon-link charmian" href="#" @click.prevent="handleIconClick('charmian')">
                        <i class="fab fa-facebook cont-icon"></i>
                    </a>
                    <a class="icon-link erciyuanman" href="#" @click.prevent="handleIconClick('erciyuanman')">
                        <i class="fab fa-instagram cont-icon"></i>
                    </a>
                    <a class="icon-link penguin" href="#" @click.prevent="handleIconClick('penguin')">
                        <i class="fab fa-github cont-icon"></i>
                    </a>
                    <a class="icon-link xunhao" hreaf="#" @click.prevent="handleIconClick('xunhao')">
                        <i class="fab fa-github cont-icon"></i>
                    </a>
                </div>

                <form id="login" class="input-group"
                    :style="{ left: isLogin ? '13px' : '-500px', marginTop: isLogin ? '40px' : '10px' }"
                    @submit.prevent="handleLogin">
                    <input v-model="loginForm.email" type="email" class="input-field" placeholder="Email" required />
                    <input v-model="loginForm.password" type="password" class="input-field" placeholder="Password"
                        required />
                    <input type="submit" class="submit-btn" :style="{ marginTop: isLogin ? '40px' : '10px' }"
                        value="Log in" />
                </form>

                <form id="register" class="input-group"
                    :style="{ left: isLogin ? '500px' : '13px', marginTop: !isLogin ? '10px' : '40px' }"
                    @submit.prevent="handleRegister">
                    <input v-model="registerForm.userName" type="text" class="input-field" placeholder="Username"
                        required />
                    <input v-model="registerForm.email" type="email" class="input-field" placeholder="Email" required />
                    <div class="code-container">
                        <input v-model="registerForm.code" type="text" class="input-field code-input"
                            placeholder="Verification Code" required />
                        <button type="button" class="code-btn" @click="handleSendCode" :disabled="cooldown > 0">
                            {{ cooldown > 0 ? `${cooldown}s` : '发送验证码' }}
                        </button>
                    </div>
                    <input v-model="registerForm.password" type="password" class="input-field" placeholder="Password"
                        required />
                    <input type="submit" class="submit-btn" value="Register" />
                </form>
            </div>

        </div>
    </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/UserStore'
import { userLogin, userRegister, sendCode, getUserInfo } from '@/api/User'
import { ElMessage } from 'element-plus'
import pinia from '@/stores'

const router = useRouter()
const userStore = useUserStore(pinia)

const isLogin = ref(true)
const cooldown = ref(0)

const loginForm = ref({
    email: '',
    password: ''
})

const registerForm = ref({
    userName: '',
    email: '',
    password: '',
    code: ''
})


// 定义社交链接映射
const socialLinks = {
    charmian: 'https://gitee.com/li-zecan',
    erciyuanman: 'https://gitee.com/wyb2728',
    penguin: 'https://gitee.com/penguin3',
    xunhao: 'https://github.com/xunhao'
}

// 点击处理方法
const handleIconClick = (type) => {
    const url = socialLinks[type]
    if (url) {
        window.open(url, '_blank') // 在新标签页中打开链接
    }
}


const handleSendCode = async () => {
    if (!registerForm.value.email) {
        ElMessage.error('请输入邮箱')
        return
    }

    try {
        const res = await sendCode({
            email: registerForm.value.email,
            type: 0
        })

        if (res.status === 'success') {
            ElMessage.success('验证码已发送')
            cooldown.value = 60
            const timer = setInterval(() => {
                cooldown.value--
                if (cooldown.value <= 0) {
                    clearInterval(timer)
                }
            }, 1000)
        } else {
            ElMessage.error(res.message || '发送失败')
        }
    } catch (error) {
        console.error('发送验证码错误:', error)
        ElMessage.error('发送验证码失败，请稍后重试')
    }
}
const handleLogin = async () => {
    try {
        if (!loginForm.value.email || !loginForm.value.password) {
            ElMessage.error('请填写完整的登录信息')
            return
        }

        const loginData = {
            email: loginForm.value.email.trim(),
            password: loginForm.value.password
        }

        // 1. 登录请求
        const loginResponse = await userLogin(loginData)

        if (loginResponse.status === 'success') {
            // 2. 设置token
            userStore.setToken(loginResponse.data.token)

            // 3. 获取完整用户信息
            const userInfoResponse = await getUserInfo(loginResponse.data.userId)

            if (userInfoResponse.status === 'success') {
                // 4. 设置完整用户信息
                await userStore.setUserInfo({
                    userId: loginResponse.data.userId,
                    username: userInfoResponse.data.username,
                    email: userInfoResponse.data.email,
                    avatarUrl: userInfoResponse.data.avatarUrl,
                    isAdmin: userInfoResponse.data.isAdmin,
                    createdAt: userInfoResponse.data.createdAt,
                    lastLogin: userInfoResponse.data.lastLogin
                })

                ElMessage.success('登录成功')
                router.push('/home')
            } else {
                throw new Error('获取用户信息失败')
            }
        } else {
            throw new Error(loginResponse.message || '登录失败')
        }
    } catch (error) {
        console.error('登录错误:', error)
        ElMessage.error(error.message || '登录失败，请稍后重试')
    }
}
const handleRegister = async () => {
    try {
        const res = await userRegister(registerForm.value)
        if (res.status === 'success') {
            ElMessage.success('注册成功，请登录')
            isLogin.value = true
            registerForm.value = { userName: '', email: '', password: '', code: '' }
        } else {
            ElMessage.error(res.message || '注册失败')
        }
    } catch (error) {
        console.error('注册错误:', error)
        ElMessage.error('注册失败，请稍后重试')
    }
}
</script>

<style scoped>
body {
    margin: 0;
    padding: 0;
    min-height: 100vh;
    font-family: sans-serif;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
}

.hero {
    min-height: 100vh;
    width: 100%;
    background-image: url('@/assets/back2.jpg');
    background-position: center;
    background-size: cover;
    position: absolute;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.hero-text {
    font-size: 7rem;
    width: 60%;
    color: white;
    margin-left: 5%;
    background: rgba(255, 255, 255, 0.2);
    /* 白色背景，透明度90% */
    padding: 20px;
    border-radius: 20px;
    text-align: center;
}

div {
    box-sizing: border-box;
}

/*main box*/
.main-box {
    width: 320px;
    height: 520px;
    position: relative;
    background: #1b1b1b;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    margin-right: 15%;
    margin-left: auto;
    border-radius: 20px;
}

.sp {
    position: absolute;
    transition: 0.5s;
}

.sp-t {
    top: 0;
    right: 0;
    width: 200px;
    height: 2px;
    background: linear-gradient(to left, transparent, #57AAB4, #57AAB4);
    animation: anim2 2s linear infinite;
    transform: translateY(-300%);
    animation-delay: 0.8s;
}

.sp-r {
    bottom: 0;
    right: 0;
    width: 2px;
    height: 200px;
    background: linear-gradient(to top, transparent, #57AAB4, #57AAB4);
    animation: anim1 2s linear infinite;
    animation-delay: 0s;
}

.sp-b {
    right: 0;
    bottom: 0;
    width: 200px;
    height: 2px;
    background: linear-gradient(to left, transparent, #57AAB4, #57AAB4);
    animation: anim2 2s linear infinite;
}

.sp-l {
    left: 0;
    top: 0;
    width: 2px;
    height: 200px;
    background: linear-gradient(to top, transparent, #57AAB4, #57AAB4);
    animation: anim1 2s linear infinite;
    animation-delay: 1s;
    transform: translateX(-300%);
}

@keyframes anim1 {
    0% {
        transform: translateY(300%);
    }

    100% {
        transform: translateY(-300%);
    }
}

@keyframes anim2 {
    0% {
        transform: translateX(300%);
    }

    100% {
        transform: translateX(-300%);
    }
}

/*main box end*/

.form-box {
    width: 316px;
    height: 520px;
    position: relative;
    background: #252525;
    padding: 5px;
    overflow: hidden;
    z-index: 5;
    opacity: 0.9;
    display: flex;
    flex-direction: column;
}

.top-section {
    flex: 0 0 auto;
    padding-bottom: 20px;
}

#after {
    width: 50%;
    height: 100%;
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    background: #2d2e30;
    z-index: -1;
    transition: 0.5s;
}

.button-box {
  width: 220px;
  margin: 35px auto 30px;
  position: relative;
  border-radius: 30px;
  display: flex;
  background: #1b1b1b;
  padding: 3px;
  justify-content: space-around;
  animation: animBTN 5s linear infinite;
}
@keyframes animBTN {
    0% {
        box-shadow: 0 0 10px 9px rgba(3, 169, 244, 0.3);
    }

    33% {
        box-shadow: 0 0 10px 9px rgba(244, 65, 165, 0.3);
    }

    66.9% {
        box-shadow: 0 0 10px 9px rgba(255, 235, 59, 0.3);
    }

    100% {
        box-shadow: 0 0 10px 9px rgba(3, 169, 244, 0.3);
    }
}

.toggle-btn {
    flex: 1;
    padding: 10px 15px;
    cursor: pointer;
    background: transparent;
    border: none;
    outline: none;
    position: relative;
    font-size: 14px;
    font-weight: bold;
    color: rgb(234, 234, 235);
    transition: all 0.5s;
    z-index: 2;
}

.toggle-btn.active {
    color: #fff;
}

.toggle-btn:hover {
    transform: scale(0.95);
}




.btn-slide {
    position: absolute;
    top: 0;
    width: 50%;
    height: 100%;
    background: linear-gradient(to right, #03a9f4, #57AAB4);
    border-radius: 30px;
    transition: all 0.5s ease;
    z-index: 1;
}

#btn {
    position: absolute;
    top: 0;
    left: 0;
    width: 110px;
    height: 100%;
    background: linear-gradient(to left, #57AAB4, #57AAB4);
    border-radius: 30px;
    transition: 0.5s;
}

.social-icons {
    margin: 0 auto;
    text-align: center;
    display: flex;
    justify-content: center;
    margin-top: 20px;
}

.icon-link {
    display: flex;
    background: transparent;
    width: 50px;
    height: 50px;
    justify-content: center;
    align-items: center;
    text-decoration: none;
    border-radius: 50%;
    margin: 0px 10px;
    box-shadow: 6px 6px 10px -1px rgba(234, 234, 235, 0.1),
        -6px -6px 10px -1px rgba(37, 37, 37, 0.7);
    border: 1px solid rgba(234, 234, 235, 0.09);
    transition: transform 0.5s;
}

.penguin {
    background-image: url('@/assets/penguin.png');
    background-size: cover;
    /* 覆盖整个区域 */
    background-position: center;
    /* 居中显示 */
    background-repeat: no-repeat;
    /* 不重复 */
}

.charmian {
    background-image: url('@/assets/charmian.png');
    background-size: cover;
    /* 覆盖整个区域 */
    background-position: center;
    /* 居中显示 */
    background-repeat: no-repeat;
    /* 不重复 */
}

.erciyuanman {
    background-image: url('@/assets/erciyuanman.jpg');
    background-size: cover;
    /* 覆盖整个区域 */
    background-position: center;
    /* 居中显示 */
    background-repeat: no-repeat;
    /* 不重复 */
}

.xunhao {
    background-image: url('@/assets/xunhao.jpg');
    background-size: cover;
    /* 覆盖整个区域 */
    background-position: center;
    /* 居中显示 */
    background-repeat: no-repeat;
    /* 不重复 */
}

.cont-icon {
    color: #57AAB4;
    font-size: 28px;
    transition: transform 0.5s;
}

.icon-link:hover {
    box-shadow: inset 4px 4px 6px -1px rgba(234, 234, 235, 0.2),
        inset 4px 4px 6px -1px rgba(37, 37, 37, 0.7),
        0 0 5px #57AAB4,
        0 0 25px #57AAB4,
        0 0 50px #57AAB4,
        0 0 100px #03e9f4;
    transform: translateY(2px);
}

.icon-link:hover .cont-icon {
    transform: scale(0.95);
}

.icon-link:hover .fa-facebook {
    color: #57AAB4;
}

.icon-link:hover .fa-instagram {
    color: #57AAB4;
}

.icon-link:hover .fa-github {
    color: #57AAB4;
}

.input-group {
    position: absolute;
    width: 280px;
    transition: all 0.5s;
    top: 180px;
}

.input-group form {
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: 100%;
}

.input-field {
    width: 100%;
    padding: 10px 5px;
    margin: 10px 0;
    border-top: 0;
    border-left: 2px solid #57AAB4;
    border-right: 0;
    border-bottom: 2px solid #57AAB4;
    outline: none;
    background: transparent;
    color: rgb(234, 234, 235);
    font-size: 15px;
    transition: 0.5s;
    border-radius: 10px;
}

.input-field:focus {
    border-left: 2px solid transparent;
    border-bottom: 2px solid transparent;
    animation: animINP 5s linear infinite, animBTN 5s linear infinite;
}

.submit-btn {
    width: 85%;
    padding: 10px 30px;
    cursor: pointer;
    display: block;
    margin: 30px auto 0 auto;
    background: linear-gradient(to right, #03a9f4, #57AAB4, #03a9f4);
    border: 0;
    outline: none;
    border-radius: 30px;
    position: relative;
    z-index: 5;
    box-sizing: border-box;
    color: #fff;
    font-weight: bold;
    font-size: 15px;
    transition: 0.5s;
    margin-top: 15px;
}

.submit-btn:hover {
    background: #57AAB4;
    color: #fff;
    border-radius: 30px;
    box-shadow: 0 0 5px #57AAB4,
        0 0 25px #57AAB4,
        0 0 50px #57AAB4,
        0 0 100px #03e9f4;
}

.span {
    margin: 20px 0;
    color: rgb(234, 234, 235);
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.check-box {
    margin: 0 10px;
    padding: 0;
}

#login {
    left: 20px;
    /* 初始状态向右偏移 */
    transition: all 0.5s;
}

#register {
    left: 520px;
    /* 初始状态也向右偏移相同距离 */
    transition: all 0.5s;
}

@keyframes a {
    0% {
        background-position: 0%;
    }

    100% {
        background-position: 400%;
    }
}

@keyframes animBTN {
    0% {
        box-shadow: 0 0 10px 9px rgba(3, 169, 244, 0.3);
    }

    33% {
        box-shadow: 0 0 10px 9px rgba(244, 65, 165, 0.3);
    }

    66.9% {
        box-shadow: 0 0 10px 9px rgba(255, 235, 59, 0.3);
    }

    100% {
        box-shadow: 0 0 10px 9px rgba(3, 169, 244, 0.3);
    }
}

.code-container {
    width: 100%;
    display: flex;
    gap: 10px;
    align-items: center;
}

.code-input {
    flex: 1;
    margin: 10px 0;
}

.code-btn {
    padding: 8px 15px;
    background: linear-gradient(to right, #03a9f4, #57AAB4);
    border: none;
    border-radius: 20px;
    color: white;
    cursor: pointer;
    font-size: 14px;
    white-space: nowrap;
    transition: all 0.3s ease;
}

.code-btn:hover:not(:disabled) {
    background: #57AAB4;
    box-shadow: 0 0 5px #57AAB4,
        0 0 15px #57AAB4;
}

.code-btn:disabled {
    background: #666;
    cursor: not-allowed;
    opacity: 0.7;
}
</style>